import {
  PageListComponentCode,
} from '../constants/code/tableConfigCode';
import { transformProperty, prettyPrint } from './codemod';
import camelCase from 'lodash/camelCase';
import upperFirst from 'lodash/upperFirst';
import { ISendAst } from '../interface/project';



const replaceStr = (regex: string, str: string, replaceString: string) => {
  const reg = new RegExp(regex, 'g');
  /* str.match(reg) */
  return str.replace(reg, replaceString);
};
const replacePageListComponentCode = (
  sendData: ISendAst,
  configCodeStr: string
) => {
  let modelCode = '';
  if (sendData.ast.tableDataSourceAst && sendData.ast.tableBaseConfigAst) {
    modelCode= sendData.ast.tableDataSourceAst.model
  }
  return {
    code: configCodeStr,
    model: modelCode,
  };
};
interface TablePageList {
  tablePageCode: string;
  tableModelCode: string;
}
export const createTableListPage = (sendData: ISendAst): TablePageList => {
  /* const componentName = upperFirst(camelCase(sendData.componentName)); */

  const createTableComponent = () => {
    /** 创建列表页组件 ================================== */
    let str = PageListComponentCode;
    const code = replacePageListComponentCode(sendData, str);
    str = code.code;
    return {
      code: str,
      model: code.model,
    };
  };
  const TableComponent = createTableComponent();
  return {
    tablePageCode:TableComponent.code,
    tableModelCode: prettyPrint(TableComponent.model),
  };
};
